import {
	baseUrl
} from "./request"

// 时间日期补零
function addZero(num) {
	return num < 10 ? num = `0${num}` : num
}

// 获取日期
export const getDate = (date) => {
	date = new Date(date)
	const year = date.getFullYear()
	const month = date.getMonth() + 1
	const day = date.getDate()
	return `${year}-${addZero(month)}-${addZero(day)}`
}

// 获取时间
export const getTime = (date, hideSecond = false) => {
	date = new Date(date)
	const hour = date.getHours()
	const minute = date.getMinutes()
	const second = date.getSeconds()
	return hideSecond ? `${addZero(hour)}:${addZero(minute)}` :
		`${addZero(hour)}:${addZero(minute)}:${addZero(second)}`
}

// 获取日期时间
export const getDateTime = (date, hideSecond = false) => {
	return `${getDate(date)} ${getTime(date,hideSecond)}`
}

// 选择并上传文件
export const uploadFile = (options) => {
	return new Promise((resolve, reject) => {
		// #ifdef H5
		uni.chooseFile({
			success: (res) => {
				console.log("选择的文件", res)
				let tempFilePaths = res.tempFilePaths;
				let tempFiles = res.tempFiles;
				uni.showLoading({
					title: '上传中...',
					mask: true,
				})
				let p = new Array;
				for (let k in tempFilePaths) {
					let pro = new Promise(function(resolve, reject) {
						uni.uploadFile({
							url: baseUrl + "/upload/file",
							filePath: tempFilePaths[k],
							header: {
								token: uni.getStorageSync('token'),
								apptype: 'merchant_xcc',
								'Content-Type': 'application/json; charset=utf-8'
							},
							name: 'file',
							formData: {},
							success: (res) => {
								if(res.statusCode == 200 ){
									let response = JSON.parse(res.data);
									if(response.code == 1){
										resolve({
											filename: tempFiles[k].name,
											url: response.data.url
										});
									}else{
										reject(res);
									}
								}else{
									reject(res);
								}
							},
							fail: (res) => {
								reject(res);
							}
						})
					})
					p.push(pro);
				};
				Promise.all(p).then((result) => {
					uni.hideLoading();
					uni.showToast({
						title: '上传成功'
					})
					// console.log("文件", result)
					resolve(result);
				}).catch(res => {
					uni.showToast({
						title: '上传失败',
						icon: 'none'
					})
				}).finally(()=>{
					uni.hideLoading()
				})
			}
		})
		// #endif
		
		// #ifdef MP-WEIXIN
		wx.chooseMessageFile({
			success: (res) => {
				console.log("选择的文件", res)
				let tempFiles = res.tempFiles;
				let tempFilePaths = [];
				tempFiles.forEach(item => {
					tempFilePaths.push(item.path)
				})
				uni.showLoading({
					title: '上传中...',
					mask: true,
				})
				let p = new Array;
				for (let k in tempFilePaths) {
					let pro = new Promise(function(resolve, reject) {
						uni.uploadFile({
							url: baseUrl + "/upload/file",
							filePath: tempFilePaths[k],
							header: {
								token: uni.getStorageSync('token'),
								apptype: 'merchant_xcc',
								'Content-Type': 'application/json; charset=utf-8'
							},
							name: 'file',
							formData: {},
							success: (res) => {
								if(res.statusCode == 200 ){
									let response = JSON.parse(res.data);
									if(response.code == 1){
										resolve({
											filename: tempFiles[k].name,
											url: response.data.url
										});
									}else{
										reject(res);
									}
								}else{
									reject(res);
								}
							},
							fail: (res) => {
								reject(res);
							}
						})
					})
					p.push(pro);
				};
				Promise.all(p).then((result) => {
					uni.hideLoading();
					uni.showToast({
						title: '上传成功'
					})
					// console.log("文件", result)
					resolve(result);
				}).catch(res => {
					uni.showToast({
						title: '上传失败',
						icon: 'none'
					})
				}).finally(()=>{
					uni.hideLoading()
				})
			}
		})
		// #endif
	})
}


// 选择并上传文件
export const uploadImage = (options) => {
	return new Promise((resolve, reject) => {
		uni.chooseImage({
			// options: options,
			success: (res) => {
				// console.log("选择的文件", res)
				let tempFilePaths = res.tempFilePaths;
				let tempFiles = res.tempFiles;
				uni.showLoading({
					title: '上传中...', 
					mask: true,
				})
				let p = new Array;
				for (let k in tempFilePaths) {
					let pro = new Promise(function(resolve, reject) {
						uni.uploadFile({
							url: baseUrl + "/upload/file",
							filePath: tempFilePaths[k],
							header: {
								token: uni.getStorageSync('token'),
								apptype: 'merchant_xcc',
								'Content-Type': 'application/json; charset=utf-8'
							},
							name: 'file',
							formData: {},
							success: (res) => {
								if(res.statusCode == 200 ){
									let response = JSON.parse(res.data);
									if(response.code == 1){
										resolve({
											filename: tempFiles[k].name,
											url: response.data.url
										});
									}else{
										
										reject(res);
									}
								}else{
									reject(res);
								}
							},
							fail: (res) => {
								reject(res);
							}
						})
					})
					p.push(pro);
				};
				Promise.all(p).then((result) => {
					uni.hideLoading();
					uni.showToast({
						title: '上传成功'
					})
					// console.log("文件", result)
					resolve(result);
				}).catch(res => {
					uni.showToast({
						title: '上传失败',
						icon: 'none'
					})
				}).finally(()=>{
					uni.hideLoading()
				})
			}
		})
	})
}

export default {
	getDateTime,
	getDate,
	getTime,
	uploadFile,
	uploadImage
}